package com.wclass.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.wclass.entity.PostsInfo;
import com.wclass.entity.PostsLike;
import com.wclass.entity.PostsReply;
import com.wclass.mapper.PostsInfoMapper;
import com.wclass.mapper.PostsReplyMapper;
import com.wclass.service.IPostsInfoService;
import com.wclass.service.IPostsReplyService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 帖子回复 服务实现类
 * </p>
 *
 * @author SEN
 * @since 2018-12-27
 */
@Service
public class PostsReplyServiceImpl extends ServiceImpl<PostsReplyMapper, PostsReply> implements IPostsReplyService {


    @Autowired
    private IPostsInfoService iPostsInfoService;

    @Override
    public boolean insert(PostsReply entity) {
        if (entity.getPostsId() != null) {
            //更新评论
            PostsInfo postsInfo = new PostsInfo();
            postsInfo.setCommentNum(baseMapper.selectCount(new EntityWrapper<PostsReply>().eq("posts_id",
                    entity.getPostsId())) + 1);
            postsInfo.setId(entity.getPostsId());
            iPostsInfoService.updateById(postsInfo);
        }
        return super.insert(entity);
    }

    @Override
    public Page<PostsReply> selectUnreadPostsReplyPage(Page<PostsReply> page, Long userId) {
        return page.setRecords(this.baseMapper.selectUnreadPostsReplyPage(page, userId));
    }

    @Override
    public Integer selectUnreadPostsReplyCount(Long userId) {
        return this.baseMapper.selectUnreadPostsReplyCount(userId);
    }

}
